<?php

namespace App\V3\V3_2_7\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class MaterielsTypeModel extends Model
{


    use HasFactory;

    protected $table = 'materiel_type';
    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function productContent()
    {
        return self::hasMany(MaterielProductContentModel::class, 'product_id', 'id');
    }

    //商品编号查重-修改
    static public function checkMaterielTypeCode($data)
    {
        return self::where([
            'materiel_no' => $data['materiel_no'],
            'main_id' => $data['main_id'],
            'delete_flag' => 0
        ])->where('id', '!=', $data['id'])->first();
    }

    static public function checkMaterielTypeRandCode($data)
    {
        return self::where([
            'materiel_no' => $data['materiel_no'],
            'main_id' => $data['main_id'],
            'delete_flag' => 0
        ])->first();
    }

    //商品信息查重
    static public function checkMaterielInfo($data)
    {
        return self::where([
            'materiel_name' => $data['materiel_name'],
            'type_name' => $data['type_name'],
            'main_id' => $data['main_id'],
            'materiel_class_id' => $data['materiel_class_id'],
            'delete_flag' => 0,
        ])
            ->first();
    }

    public static function getMaterielInfo($id)
    {
        return self::where([
            'id' => $id,
        ])->first();
    }

    //获取指定商品详细信息
    static public function info($id)
    {
        return self::select(
            'a.id',
            'a.materiel_name',
            'a.type_name',
            'a.materiel_img',
            'a.materiel_no',
            'a.materiel_class_id',
            'a.remark',
            'a.length',
            'a.width',
            'a.height',
            'a.weight',
            'a.attribute',
            'a.cost',
            'a.selling_price',
            'a.lower_selling_price',
            'a.is_album',
            'a.materiel_unit',
            'a.lower_selling_price',
            'b.class_name as sec_class_name',
            'c.id as fir_class_id',
            'c.class_name as fir_class_name',
            'a.is_use_serial'
        )
            ->from('materiel_type as a')
            ->leftJoin('materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
            ->leftJoin('materiel_class as c', 'b.parent_id', '=', 'c.id')
            ->where('a.id', $id)
            ->where('a.delete_flag', 0)
            ->first();
    }

    //物料商品列表
    static public function materielTypeSelect($mainId, $keyword, $condition, $pageSize, $currentPage, $isProductMateriel, $data = [])
    {
        $query = self::select(
            'a.id',
            'materiel_name',
            'type_name',
            'materiel_img',
            'materiel_no',
            'materiel_class_id',
            'b.class_name as sec_class_name',
            'c.id as fir_class_id',
            'c.class_name as fir_class_name',
            'a.remark',
            'a.length',
            'a.width',
            'a.height',
            'a.weight',
            'a.attribute',
            'a.cost',
            'a.selling_price',
            'a.is_use_serial',
            'a.materiel_unit',
            'a.lower_selling_price',
            'a.is_product_materiel',
            'a.goods_client_name',
            'a.goods_supplier_name',
            'a.goods_storage_name'
        )
            ->from('materiel_type as a')
            ->leftJoin('materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
            ->leftJoin('materiel_class as c', 'b.parent_id', '=', 'c.id')
            ->where($condition);
        if (isset($isProductMateriel)) {
            $query->where('a.is_product_materiel', $isProductMateriel);
        }

        if (!empty($data['f_class_id'])) {
            $secClassId = MaterielClassModel::query()
                ->where([
                    'delete_flag' => 0,
                    'main_id' => $mainId,
                    'parent_id' => $data['f_class_id']
                ])->pluck('id');
            if (!empty($secClassId->toArray())) {
                $secClassId = $secClassId->toArray();
                $query->whereIn('a.materiel_class_id', $secClassId);
            }
        }
        $query = $query->where(function ($query) use ($keyword) {
            if (!empty($keyword)) {
                $query->orWhere('a.materiel_name', 'like', '%' . $keyword . '%');
                $query->orWhere('a.type_name', 'like', '%' . $keyword . '%');
                $query->orWhere('a.materiel_no', 'like', '%' . $keyword . '%');
                $query->orWhere('b.class_name', 'like', '%' . $keyword . '%');
                $query->orWhere('c.class_name', 'like', '%' . $keyword . '%');
            }
        })
            ->where([
                'a.delete_flag'=>0,
                'b.delete_flag'=>0,
                'c.delete_flag'=>0,
            ])
            ->where('a.main_id', $mainId)
            ->orderBy('a.id', 'DESC')
            ->paginate($pageSize, ['*'], '', $currentPage);
        return $query;
    }

    //物料商品列表
    static public function selectGoodsList($mainId, $keyword, $condition, $pageSize, $currentPage, $isProductMateriel, $data = [])
    {
        $query = self::query()->select(
            'a.id',
            'materiel_name',
            'type_name',
            'materiel_img',
            'materiel_no',
            'materiel_class_id',
            'b.class_name as sec_class_name',
            'c.id as fir_class_id',
            'c.class_name as fir_class_name',
            'a.remark',
            'a.length',
            'a.width',
            'a.height',
            'a.weight',
            'a.attribute',
            'a.cost',
            'a.selling_price',
            'a.is_use_serial',
            'a.materiel_unit',
            'a.lower_selling_price',
            'a.is_product_materiel',
            'a.goods_client_name',
            'a.goods_supplier_name'
        )
            ->from('materiel_type as a')
            ->leftJoin('materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
            ->leftJoin('materiel_class as c', 'b.parent_id', '=', 'c.id')
            ->where($condition);
        if (isset($isProductMateriel)) {
            $query->where('a.is_product_materiel', $isProductMateriel);
        }
        if (!empty($data['f_class_id'])) {
            $secClassId = MaterielClassModel::query()
                ->where([
                    'delete_flag' => 0,
                    'main_id' => $mainId,
                    'parent_id' => $data['f_class_id']
                ])->pluck('id');
            if (!empty($secClassId->toArray())) {
                $secClassId = $secClassId->toArray();
                $query->whereIn('a.materiel_class_id', $secClassId);
            }
        }
        $query = $query->where(function ($query) use ($keyword) {
            if (!empty($keyword)) {
                $query->orWhere('a.materiel_name', 'like', '%' . $keyword . '%');
                $query->orWhere('a.type_name', 'like', '%' . $keyword . '%');
                $query->orWhere('a.materiel_no', 'like', '%' . $keyword . '%');
            }
        })
            ->where([
                'a.delete_flag'=> 0,
                'b.delete_flag'=> 0,
                'c.delete_flag'=> 0,
            ])
            ->where('a.main_id', $mainId)
            ->groupBy('materiel_name')
            ->orderBy('a.id', 'DESC')
            ->paginate($pageSize, ['*'], '', $currentPage)->toArray();
        return $query;
    }
    //物料商品列表-含库存信息
    //物料类型列表

    static public function getMaterielTypeList($user, $condition, $keyword, $data, $typeId)
    {
        $query = StorageManageModel::select(
            'b.*',
            'c.id as s_class_id',
            'c.class_name as s_class_name',
            'c.class_no as s_class_no',
            'd.class_name as f_class_name',
            'd.class_no as f_class_no',
            'd.id as f_class_id',
            'a.in_storage_num',
            'a.storage_id',
            'e.storage_name',
            'b.length',
            'b.width',
            'b.height',
            'b.weight',
            'b.attribute',
            'b.cost',
            'a.shelf_id',
            'a.location_id',
            's.shelf_name',
            'l.location_name',
            'product_use_tag',
            'a.id as storage_manage_id',
            'b.goods_supplier_name',
            'b.goods_client_name',
            'b.goods_storage_name'
        )
            ->from('storage_manage as a')
            ->leftJoin('materiel_type AS b', 'b.id', '=', 'a.type_id')
            ->leftJoin('materiel_class AS c', 'c.id', '=', 'b.materiel_class_id')
            ->leftJoin('materiel_class AS d', 'd.id', '=', 'c.parent_id')
            ->leftJoin('storage as e', 'e.id', '=', 'a.storage_id')
            ->leftJoin('storage_shelf as s', 'a.shelf_id', '=', 's.id')
            ->leftJoin('storage_shelf_location as l', 'a.location_id', '=', 'l.id')
            ->where($user)
            ->where('a.delete_flag', 0)
            ->where('b.delete_flag', 0)
            ->where('e.delete_flag', 0)
            ->where('c.delete_flag', 0)
            ->where('d.delete_flag', 0)
            ->where($condition);
        if (!empty($data['storage_id'])) {
            $query->where('a.storage_id', $data['storage_id']);
        }
        if (!empty($data['f_class_id'])) {
            $secClassId = MaterielClassModel::query()
                ->where([
                    'delete_flag' => 0,
                    'main_id' => $data['main_id'],
                    'parent_id' => $data['f_class_id']
                ])->pluck('id');
            if (!empty($secClassId->toArray())) {
                $secClassId = $secClassId->toArray();
                $query->whereIn('b.materiel_class_id', $secClassId);
            }
        }
        $query->where(function ($query) use ($keyword) {
            if (!empty($keyword)) {
                $query->orWhere('b.materiel_name', 'like', '%' . $keyword . '%');
                $query->orWhere('b.type_name', 'like', '%' . $keyword . '%');
                $query->orWhere('b.materiel_no', 'like', '%' . $keyword . '%');
                $query->orWhere('c.class_name', 'like', '%' . $keyword . '%');
                $query->orWhere('d.class_name', 'like', '%' . $keyword . '%');
            }
        });

        if (!empty($typeId)) {
            $query->whereIn('a.type_id', $typeId);
        }

        if (!empty($query->get())) {
            return $query->get()->toArray();
        } else {
            return [];
        }
    }

    static public function chooseGoodsList($user, $condition, $keyword, $data, $typeId)
    {
        $query = StorageManageModel::select(
            'b.*',
            'c.id as s_class_id',
            'c.class_name as s_class_name',
            'c.class_no as s_class_no',
            'd.class_name as f_class_name',
            'd.class_no as f_class_no',
            'd.id as f_class_id',
            'a.in_storage_num',
            'a.storage_id',
            'e.storage_name',
            'b.length',
            'b.width',
            'b.height',
            'b.weight',
            'b.attribute',
            'b.cost',
            'a.shelf_id',
            'a.location_id',
            's.shelf_name',
            'l.location_name',
            'product_use_tag',
            'a.id as storage_manage_id'
        )
            ->from('storage_manage as a')
            ->leftJoin('materiel_type AS b', 'b.id', '=', 'a.type_id')
            ->leftJoin('materiel_class AS c', 'c.id', '=', 'b.materiel_class_id')
            ->leftJoin('materiel_class AS d', 'd.id', '=', 'c.parent_id')
            ->leftJoin('storage as e', 'e.id', '=', 'a.storage_id')
            ->leftJoin('storage_shelf as s', 'a.shelf_id', '=', 's.id')
            ->leftJoin('storage_shelf_location as l', 'a.location_id', '=', 'l.id')
            ->where($user)
            ->where('a.delete_flag', 0)
            ->where('b.delete_flag', 0)
            ->where('e.delete_flag', 0)
            ->where('c.delete_flag', 0)
            ->where('d.delete_flag', 0)
            ->where($condition);
        if (!empty($data['storage_id'])) {
            $query->where('a.storage_id', $data['storage_id']);
        }
        if (!empty($data['f_class_id'])) {
            $secClassId = MaterielClassModel::query()
                ->where([
                    'delete_flag' => 0,
                    'main_id' => $data['main_id'],
                    'parent_id' => $data['f_class_id']
                ])->pluck('id');
            if (!empty($secClassId->toArray())) {
                $secClassId = $secClassId->toArray();
                $query->whereIn('b.materiel_class_id', $secClassId);
            }
        }
        $query->where(function ($query) use ($keyword) {
            if (!empty($keyword)) {
                $query->orWhere('b.materiel_name', 'like', '%' . $keyword . '%');
                $query->orWhere('b.type_name', 'like', '%' . $keyword . '%');
                $query->orWhere('b.materiel_no', 'like', '%' . $keyword . '%');
                $query->orWhere('c.class_name', 'like', '%' . $keyword . '%');
                $query->orWhere('d.class_name', 'like', '%' . $keyword . '%');
            }
        });

        if (!empty($typeId)) {
            $query->whereIn('a.type_id', $typeId);
        }

        if (!empty($query->get())) {
            return $query->get()->toArray();
        } else {
            return [];
        }
    }

    //获取物料商品信息
    static public function getTypeInfo($typeId, $mainId)
    {
        return self::select(
            'a.id',
            'materiel_name',
            'type_name',
            'materiel_no',
            'b.id as materiel_class_id',
            'b.class_name',
            'b.class_no'
        )
            ->from('materiel_type as a')
            ->leftJoin('materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
            ->where([
                'a.id' => $typeId,
                'a.main_id' => $mainId,
                'a.delete_flag' => 0,
            ])
            ->first();
    }

    /**
     * 查询同名商品信息
     * @param unknown $goodsInfo
     * @return unknown
     */
    static public function getSameNameGoodsList($goodsInfo)
    {
        return self::select(
            'id',
            'spec_img',
            'is_use_serial'
        )->where([
            'materiel_name' => $goodsInfo['materiel_name'],
            'main_id' => $goodsInfo['main_id'],
            'materiel_class_id' => $goodsInfo['materiel_class_id'],
            'is_product_materiel' => 0,
            'delete_flag' => 0,
        ])->get();
    }
}
